<?php

/*
+--------------------------------------------------------------------------
|   Invision Power Board
|   =============================================
|   by Matthew Mecham
|   (c) 2001 - 2006 Invision Power Services, Inc.
|   
|   =============================================
|   
|   
+---------------------------------------------------------------------------
|   > $Date: 2009-05-10 11:35:56 -0400 (Sun, 10 May 2009) $
|   > $Revision: 1 $
|   > $Author: BabyWolf $
+---------------------------------------------------------------------------
|
|   > Multi Site functions
|   > Module written by BabyWolf
|   > Date started: 10th May 2009
|
|	> Module Version Number: 1.0.0
|   > DBA Checked: 
+--------------------------------------------------------------------------
*/

if ( ! defined( 'IN_ACP' ) )
{
	print "<h1>Incorrect access</h1>You cannot access this file directly. If you have recently upgraded, make sure you upgraded 'admin.php'.";
	exit();
}

class ad_sites {

	function auto_run()
	{
		$this->ipsclass->forums->forums_init();
		
		require ROOT_PATH.'sources/lib/admin_forum_functions.php';
		
		$this->forumfunc = new admin_forum_functions();
		$this->forumfunc->ipsclass =& $this->ipsclass;
		
		$this->ipsclass->admin->nav[]       = array( "{$this->ipsclass->form_code}", "Shop Management" );

		//-----------------------------------------
		// Load template
		//-----------------------------------------

		$this->html = $this->ipsclass->acp_load_template( 'cp_skin_shops' );

		

		//-----------------------------------------

		$this->ipsclass->DB->sql_get_version();

		$this->true_version  = $this->ipsclass->DB->true_version;
		$this->mysql_version = $this->ipsclass->DB->mysql_version;

		switch($this->ipsclass->input['code'])
		{
			// Category functions
			case 'sites_delete':
				$this->ipsclass->admin->cp_permission_check( $this->perm_main.'|'.$this->perm_child.':remove' );
				$this->site_delete();
				break;
			case 'sites_showedit':
				$this->ipsclass->admin->cp_permission_check( $this->perm_main.'|'.$this->perm_child.':edit' );
				$this->site_form('edit');
				break;
				
			case 'sites_new':
				$this->ipsclass->admin->cp_permission_check( $this->perm_main.'|'.$this->perm_child.':add' );
				$this->site_form('add');
				break;

			case 'sites_add':
				$this->ipsclass->admin->cp_permission_check( $this->perm_main.'|'.$this->perm_child.':add' );
				$this->site_save('add');
				break;

			case 'sites_edit':
				$this->ipsclass->admin->cp_permission_check( $this->perm_main.'|'.$this->perm_child.':edit' );
				$this->site_save('edit');
				break;

			//-----------------------------------------
			default:
				$this->ipsclass->admin->cp_permission_check( $this->perm_main.'|'.$this->perm_child.':' );
				$this->sites_start();
				break;
		}

	}

	//-----------------------------------------
	//
	// Delete setting group
	//
	//-----------------------------------------

	function site_delete()
	{
		if ( $this->ipsclass->input['id'] )
		{
			$this->ipsclass->DB->simple_exec_query( array( 'delete' => 'sites', 'where' => 'site_id='.intval($this->ipsclass->input['id']) ) );

			$this->ipsclass->main_msg = "Site Removed";
		}

		$this->sites_start();
	}

	//-----------------------------------------
	// Category Save Form
	//-----------------------------------------

	function site_save($type='add')
	{
		//-----------------------------------------
		// INIT
		//-----------------------------------------

		$site_name = $this->ipsclass->input['siteName'];
		$site_info = $this->ipsclass->input['siteInfo'];
		$site_domain = $this->ipsclass->input['siteDomain'];
		$site_portal_article_number = $this->ipsclass->input['sitePortalArticleNo'];
		$site_front_html = $this->ipsclass->input['site_front_html'];
		$site_location = $this->ipsclass->input['site_location'];
		$site_turnoff_portal = $this->ipsclass->input['site_turnoff_portal'];
		
		//-----------------------------------------
		// Check...
		//-----------------------------------------

		if ( ! $site_name )
		{
			$this->ipsclass->main_msg = "You must enter site name.";
			$this->category_form();
			return;
		}
		
		if(! $site_domain) {
			$this->ipsclass->main_msg = "You must enter site domain.";
			$this->category_form();
			return;
		}
		
		$site_skin_id = $this->ipsclass->input['siteSkin'];
		$site_forum_ids = serialize($this->ipsclass->input['siteForums']);
		
		//-----------------------------------------
		// check...
		//-----------------------------------------

		$array = array( 'site_name' 		=> $site_name,
						'site_domain' 		=> $site_domain,
						'site_info'			=> $site_info,
						'site_skin_id' 		=> $site_skin_id,
						'site_forum_ids'	=> $site_forum_ids,
						'site_portal_article_number' => $site_portal_article_number,
						'site_front_html'	=> $site_front_html,
						'site_location'		=> $site_location,
						'site_turnoff_portal' => $site_turnoff_portal
					 );

		if ( $type == 'add' )
		{
			
			$this->ipsclass->DB->do_insert( 'sites', $array );
			$this->ipsclass->main_msg = 'New Site Added';
		}
		else
		{
			
			$this->ipsclass->DB->do_update( 'sites', $array, 'site_id='.intval($this->ipsclass->input['id']) );
			$this->ipsclass->main_msg = 'New Site Edited';
		}

		$this->sites_start();
	}

	//-----------------------------------------
	//
	// New setting form
	//
	//-----------------------------------------

	function site_form( $type='add' )
	{
		$this->ipsclass->admin->page_title  = "Site Management";
		$this->ipsclass->admin->page_detail = "This section contains all the functions to manage your IPB site.";
		$this->ipsclass->admin->nav[]       = array( '', "Add/Edit Site" );

		$forum_ids = array();
		
		if ( $type == 'add' )
		{
			$formcode = 'sites_add';
			$title    = "Create New Site";
			$button   = "Add Site";
			$site = array( 	   'site_name' 		=> '',
							   'site_info'		=> '',
							   'site_domain' 	=> '',
							   'site_skin_id'	=> '',
							   'site_portal_article_number' => 6,
							   'site_front_html'=> '',
								'site_location' => 0,
								'site_turnoff_portal' => 0
							   );

		}
		else
		{
			$site = $this->ipsclass->DB->simple_exec_query( array( 'select' => '*', 'from' => 'sites', 'where' => 'site_id='.intval($this->ipsclass->input['id']) ) );
			$forum_ids = unserialize($site['site_forum_ids']);
			if(!is_array($forum_ids)) $forum_ids = array();

			if ( ! $site['site_id']) {
				$this->ipsclass->main_msg = "No ID was passed, please try again.";
				$this->site_start();
			}

			$formcode = 'sites_edit';
			$title    = "Edit Site<u>".$site['site_name']."</u>";
			$button   = "Edit Site";
		}

		$this->ipsclass->admin->page_detail = '&nbsp;';
		$this->ipsclass->admin->page_title  = $title;

		//-----------------------------------------
		// Build forum list
		//-----------------------------------------
		$forum_html = "<select name='siteForums[]' class='textinput' size='15' multiple='multiple'>\n";
		
		$forum_html .= "<option value='all'>-- ALL FORUMS --</option>\n";		    
		
		$forum_jump = $this->forumfunc->ad_forums_forum_data();

		foreach ( $forum_jump as $i )
		{
			if ( in_array($i['id'], $forum_ids) )
			{
				$selected = ' selected="selected"';
			}
			else
			{
				$selected = "";
			}
			
			if ( isset($i['redirect_on']) AND $i['redirect_on'] == 1 )
			{
				continue;
			}
			
			//$forum_jump[] = array( $i['id'], $i['depthed_name'] );
			
			$forum_html  .= "<option value=\"{$i['id']}\" $selected>{$i['depthed_name']}</option>\n";

		}
		
		$forum_html  .= "</select>";
		
		//-----------------------------------------
		// Build skin list
		//-----------------------------------------
		$this->ipsclass->DB->simple_construct(array('select'	=> '*',
													'from'		=> 'skin_sets'
													)
												);
		$this->ipsclass->DB->simple_exec();
		
		$skins = array();
		while($skin=$this->ipsclass->DB->fetch_row()) {
			$skins[] = array($skin['set_skin_set_id'],$skin['set_name']);										
		}
		
		//-----------------------------------------
		// start form
		//-----------------------------------------

		$this->ipsclass->html .= $this->ipsclass->adskin->start_form( array( 1 => array( 'code'   , $formcode ),
																			 2 => array( 'act'    , 'sites'      ),
																			 3 => array( 'id'     , $this->ipsclass->input['id'] ),
																			 4 => array( 'section', 'content' ),
																	)      );

		//-----------------------------------------
		// start table
		//-----------------------------------------

		$this->ipsclass->adskin->td_header[] = array( "{none}"  , "40%" );
		$this->ipsclass->adskin->td_header[] = array( "{none}"  , "60%" );

		//-----------------------------------------
		// um..
		//-----------------------------------------

		$this->ipsclass->html .= $this->ipsclass->adskin->start_table( $title );
												 		    	
		$this->ipsclass->html .= $this->ipsclass->adskin->add_td_row( array( "<b>Site Name</b><div style='color:gray'>Example: Even Music Site</div>" ,
												  			   $this->ipsclass->adskin->form_input( 'siteName', ( isset($_POST['siteName']) AND $_POST['siteName'] ) ? $_POST['siteName'] : $site['site_name'] )
										 		    	)      );
		
		$this->ipsclass->html .= $this->ipsclass->adskin->add_td_row( array( "<b>Site Info</b><div style='color:gray'>Example: Even Music Site</div>" ,
												  			   $this->ipsclass->adskin->form_input( 'siteInfo', ( isset($_POST['siteInfo']) AND $_POST['siteInfo'] ) ? $_POST['siteInfo'] : $site['site_info'] )
										 		    	)      );
		
		$this->ipsclass->html .= $this->ipsclass->adskin->add_td_row( array( "<b>Site Domain</b><div style='color:gray'>Example: music.even.vn</div>" ,
												  			   $this->ipsclass->adskin->form_input( 'siteDomain', ( isset($_POST['siteDomain']) AND $_POST['siteDomain'] ) ? $_POST['siteDomain'] : $site['site_domain'] )
										 		    	)      );
										 		    	
		$this->ipsclass->html .= $this->ipsclass->adskin->add_td_row( array( "<b>Site Skin</b><div style='color:gray'>Please choose skin for this site.</div>" ,
												  			   $this->ipsclass->adskin->form_dropdown( 'siteSkin', $skins, $site['site_skin_id'] )
										 		    	)      );

		$this->ipsclass->html .= $this->ipsclass->adskin->add_td_row( array( "<b>Site Forums</b><div style='color:gray'>Choose forum that will appear on this site.<br /><b>* Remember that if you want a sub forums displayed, its parent have to be chosen.</b></div>" ,
												  			   $forum_html
										 		    	)      );
		
		$this->ipsclass->html .= $this->ipsclass->adskin->add_td_row( array( "<b>Number of article on portal</b><div style='color:gray'>Default is 6</div>" ,
												  			   $this->ipsclass->adskin->form_input( 'sitePortalArticleNo', ( isset($_POST['sitePortalArticleNo']) AND $_POST['sitePortalArticleNo'] ) ? $_POST['sitePortalArticleNo'] : $site['site_portal_article_number'] )
										 		    	)      );

		$this->ipsclass->html .= $this->ipsclass->adskin->add_td_row(array( "<b>Front page embed HTML</b>",
															   $this->ipsclass->adskin->form_textarea("site_front_html" , $this->ipsclass->my_br2nl( ( isset($_POST['site_front_html']) AND $_POST['site_front_html']  )? $_POST['site_front_html'] : $site['site_front_html'] ))
															   ));
		$this->ipsclass->html .= $this->ipsclass->adskin->add_td_row( array( "<b>Member ‘s Location</b><div style='color:gray'>Default is no</div>" ,
												  			   $this->ipsclass->adskin->form_yes_no( 'site_location', ( isset($_POST['site_location']) AND $_POST['site_location'] ) ? $_POST['site_location'] : ( $site['site_location'] == 1 ? 1 : 0 ) )
										 		    	)      );
		$this->ipsclass->html .= $this->ipsclass->adskin->add_td_row( array( "<b>Turn off portal</b><div style='color:gray'>Default is no</div>" ,
												  			   $this->ipsclass->adskin->form_yes_no( 'site_turnoff_portal', ( isset($_POST['site_turnoff_portal']) AND $_POST['site_turnoff_portal'] ) ? $_POST['site_turnoff_portal'] : ( $site['site_turnoff_portal'] == 1 ? 1 : 0 ) )
										 		    	)      );
										 		    	
		$this->ipsclass->html .= $this->ipsclass->adskin->end_form( $button );

		$this->ipsclass->html .= $this->ipsclass->adskin->end_table();

		$this->ipsclass->admin->output();

	}

	//-----------------------------------------
	// Settings Start
	//-----------------------------------------

	function sites_start()
	{
		$this->ipsclass->admin->page_title  = "Sites management";
		$this->ipsclass->admin->page_detail = "This section contains all the functions to manage your IPB sites.";

		//-----------------------------------------
		// start table
		//-----------------------------------------

		$this->ipsclass->adskin->td_header[] = array( " "  , "2%" );
		$this->ipsclass->adskin->td_header[] = array( "Sites name"  , "60%" );
		$this->ipsclass->adskin->td_header[] = array( "Domain"  , "28%" );
		$this->ipsclass->adskin->td_header[] = array( "Action"  , "10%" );

		$basic_title = "<table cellpadding='0' cellspacing='0' border='0' width='100%'>
						<tr>
						 <td align='left' width='40%' style='font-size:12px; vertical-align:middle;font-weight:bold; color:#FFF;'>Sites management</td>
						</tr>
						</table>";

		$this->ipsclass->html .= $this->ipsclass->adskin->start_table($basic_title);

		//-----------------------------------------
		// Get categories
		//-----------------------------------------

		$this->ipsclass->DB->simple_construct( array( 'select' => '*', 'from' => 'sites'));
		$this->ipsclass->DB->simple_exec();

		while( $site = $this->ipsclass->DB->fetch_row() )
		{
			$this->ipsclass->html .= $this->ipsclass->adskin->add_td_row( array( "<div align='center'><img src='{$this->ipsclass->adskin->img_url}/images/settings_folder.gif' border='0' alt='Folder' /></div>",
																   "<div><a href='{$this->ipsclass->base_url}&{$this->ipsclass->form_code}&code=items_view&catid={$site['site_id']}' title='Click to manage this site'><b>{$site['site_name']}</b></a>" ,
																	"<b>{$site['site_domain']}</b>" ,
												  				   array("<div align='center' style='white-space:nowrap'>
												  				          <a href='{$this->ipsclass->base_url}&{$this->ipsclass->form_code}&code=sites_showedit&id={$site['site_id']}' title='Edit this site'><img src='{$this->ipsclass->adskin->img_url}/images/icons_menu/edit.gif' border='0' alt='Edit'  /></a>
																          <a href='{$this->ipsclass->base_url}&{$this->ipsclass->form_code}&code=sites_delete&id={$site['site_id']}' title='Delete this site'><img src='{$this->ipsclass->adskin->img_url}/images/icons_menu/delete.gif' border='0' alt='Delete'  /></a>
																          {$in_dev_extra}
																          </div>", 1, 'tablerow3' )
										 					  )      );
		}

		$this->ipsclass->html .= $this->ipsclass->adskin->end_table();


		//-----------------------------------------
		// Import partial settings?
		//-----------------------------------------

		$this->ipsclass->html .= $this->ipsclass->adskin->start_form( array( 1 => array( 'code'          , 'category_add'    ),
																 2 => array( 'act'           , 'shop'        ),
																 3 => array( 'section', $this->ipsclass->section_code ),
													 ) , "categoryaddform");

		$this->ipsclass->admin->output();
	}
}


?>